package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.UserMessage;
import com.mls.fivedistrict.server.UserMessageService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/6/05 08:49
 */
@RestController
@RequestMapping("/api/manager")
public class UserMessageController extends BaseController {
    @Autowired
    UserMessageService userMessageService;

    /**
     * 用户消息添加
     * @param userMessage
     * @return
     */
    @PostMapping("/v1/userMessage")
    public ServiceResponse addUserMessage(@RequestBody UserMessage userMessage){
        BeanValidator.check(userMessage);
        boolean insert = userMessageService.insert(userMessage);
        if (insert){
            return ServiceResponse.createSuccessResponse("添加成功!");
        }else{
            return ServiceResponse.createErrorResponse("添加失败！");
        }
    }

    /**
     * 用户消息分页列表
     * @return
     */
    @GetMapping("/v1/userMessages")
    public ServiceResponse getUserMessageList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = userMessageService.selectList(pageFilter.getWrapper());
        PageInfo<UserMessage> userMessagePageInfo = new PageInfo<>(selectList,5);
        return ServiceResponse.createSuccessResponse(userMessagePageInfo);
    }

    /**
     * 获取单个详细信息
     * @param userMessageId
     * @return
     */
    @GetMapping("/v1/userMessage/{userMessageId}")
    public ServiceResponse getUserMessageById(@PathVariable Long userMessageId){
        Optional.ofNullable(userMessageId).orElseThrow(() -> new CustomerException("消息id不能为空"));
        UserMessage userMessage = userMessageService.selectById(userMessageId);
        if(null!=userMessage){
            return ServiceResponse.createSuccessResponse(userMessage);
        }else{
            return ServiceResponse.createErrorResponse("要查询的用户消息不存在!");
        }
    }
    @PutMapping("/v1/userMessage/read/{userMessageId}")
    public ServiceResponse readUserMessage(@PathVariable Long userMessageId){
        Optional.ofNullable(userMessageId).orElseThrow(() -> new CustomerException("消息id不能为空"));
        UserMessage userMessage = new UserMessage();
        userMessage.setId(userMessageId);
        userMessage.setViewDate(new Date());
        userMessage.setRead(true);
        boolean b = userMessageService.updateById(userMessage);
        if (b){
            return ServiceResponse.createSuccessResponse("查看成功");
        }else{
            return ServiceResponse.createErrorResponse("查看失败");
        }
    }
}
